package com.kylin.educenter.controller;


import com.kylin.JwtUtils;
import com.kylin.R;
import com.kylin.educenter.entity.UcenterMember;
import com.kylin.educenter.entity.vo.RegisterVo;
import com.kylin.educenter.service.UcenterMemberService;
import com.kylin.ordervo.UcenterMemberOrder;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author kylin
 * @since 2020-07-19
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    //登录
    @PostMapping("/login")
    @ApiOperation("登录")
    public R loginUser(@RequestBody UcenterMember member){

        //调用service方法实现登录

        //返回token值，使用jwt生成
        String token = memberService.login(member);

        return R.ok().data("token",token);
    }

    //注册
    @PostMapping("/register")
    @ApiOperation("注册")
    public R registerUser(@RequestBody RegisterVo registerVo){


        memberService.register(registerVo);

        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("/getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        //根据用户id查询数据库信息
        UcenterMember member = memberService.getById(memberId);

        return R.ok().data("userInfo",member);
    }

    //根据用户id获取用户信息
    @PostMapping("/getUserInfoOrder/{id}")
    @ApiOperation("根据用户id获取用户信息")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable("id") String id){

        UcenterMember ucenterMember = memberService.getById(id);

        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();

        BeanUtils.copyProperties(ucenterMember,ucenterMemberOrder);

        return ucenterMemberOrder;
    }

    //查询某一天注册人数
    @GetMapping("/countRegister/{day}")
    @ApiOperation("查询某一天注册人数")
    public R countRegister(@PathVariable String day){
        Integer count = memberService.countRegisterDay(day);
        return R.ok().data("countRegister",count);
    }


}

